﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Test
{
    /// <summary>
    /// Interaction logic for Selector.xaml
    /// </summary>
    public partial class Selector : UserControl
    {
        private Point mousePos;
        private Shape curShape;
        private Canvas canva;

        private Selector()
        {
            InitializeComponent();
        }

        /*
         * use example:
         * curSelector = new Selector(curEllips);
         * MyCanvas.Children.Add(curSelector);
         */
        public Selector(Shape selected, Canvas canv)
        {
            InitializeComponent();
            curShape = selected;
            mousePos = new Point(-1, -1);
            canva = canv;

            this.SetValue(Canvas.LeftProperty, (double)curShape.GetValue(Canvas.LeftProperty) - 3.0);
            this.SetValue(Canvas.TopProperty, (double)curShape.GetValue(Canvas.TopProperty) - 3.0);

            this.Width = curShape.Width+6;
            this.Height = curShape.Height+6;
        }

        private void SelectorRectangle_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(this);
            e.Handled = true;
            Shape control = sender as Shape;
            control.CaptureMouse();
        }

        private void SelectorRectangle_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(this);
            curShape.SetValue(Canvas.TopProperty, (double)curShape.GetValue(Canvas.TopProperty) + (curMousePos.Y - mousePos.Y));
            curShape.SetValue(Canvas.LeftProperty, (double)curShape.GetValue(Canvas.LeftProperty) + (curMousePos.X - mousePos.X));
            this.SetValue(Canvas.TopProperty, (double)this.GetValue(Canvas.TopProperty) + (curMousePos.Y - mousePos.Y));
            this.SetValue(Canvas.LeftProperty, (double)this.GetValue(Canvas.LeftProperty) + (curMousePos.X - mousePos.X));
            e.Handled = true;
        }

        private void SelectorRectangle_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1,-1);
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
            e.Handled = true;
        }

        private void SelectorLSide_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorLSide_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Width + (mousePos.X - curMousePos.X) >= 0)
            {
                curShape.Width += (mousePos.X - curMousePos.X);
                this.Width += (mousePos.X - curMousePos.X);

                curShape.SetValue(Canvas.LeftProperty, curMousePos.X + 3.0);
                this.SetValue(Canvas.LeftProperty, curMousePos.X);
                mousePos = curMousePos;
            }
            e.Handled = true;
        }

        private void SelectorLSide_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

        private void SelectorRSide_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorRSide_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Width + (curMousePos.X - mousePos.X) >= 0)
            {
                curShape.Width += (curMousePos.X - mousePos.X);
                this.Width += (curMousePos.X - mousePos.X);
                mousePos = curMousePos;
            }
            e.Handled = true;
        }

        private void SelectorRSide_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

        private void SelectorDSide_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorDSide_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Height + (curMousePos.Y - mousePos.Y) >= 0)
            {
                curShape.Height += (curMousePos.Y - mousePos.Y);
                this.Height += (curMousePos.Y - mousePos.Y);
                mousePos = curMousePos;
            }
            e.Handled = true;
        }

        private void SelectorDSide_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

        private void SelectorUSide_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorUSide_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Height + (mousePos.Y - curMousePos.Y) >= 0)
            {
                curShape.Height += (mousePos.Y - curMousePos.Y);
                this.Height += (mousePos.Y - curMousePos.Y);
                curShape.SetValue(Canvas.TopProperty, curMousePos.Y + 3.0);
                this.SetValue(Canvas.TopProperty, curMousePos.Y);
                mousePos = curMousePos;
            }
            e.Handled = true;
        }

        private void SelectorUSide_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

        private void SelectorDRCorner_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorDRCorner_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Height + (curMousePos.Y - mousePos.Y) >= 0)
            {
                curShape.Height += (curMousePos.Y - mousePos.Y);
                this.Height += (curMousePos.Y - mousePos.Y);
                mousePos.Y = curMousePos.Y;
            }
            if (curShape.Width + (curMousePos.X - mousePos.X) >= 0)
            {
                curShape.Width += (curMousePos.X - mousePos.X);
                this.Width += (curMousePos.X - mousePos.X);
                mousePos.X = curMousePos.X;
            }
            e.Handled = true;
        }

        private void SelectorDRCorner_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

        private void SelectorURCorner_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorURCorner_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Height + (mousePos.Y - curMousePos.Y) >= 0)
            {
                curShape.Height += (mousePos.Y - curMousePos.Y);
                this.Height += (mousePos.Y - curMousePos.Y);
                curShape.SetValue(Canvas.TopProperty, curMousePos.Y + 3.0);
                this.SetValue(Canvas.TopProperty, curMousePos.Y);
                mousePos.Y = curMousePos.Y;
            }
            if (curShape.Width + (curMousePos.X - mousePos.X) >= 0)
            {
                curShape.Width += (curMousePos.X - mousePos.X);
                this.Width += (curMousePos.X - mousePos.X);
                mousePos.X = curMousePos.X;
            }
            e.Handled = true;
        }

        private void SelectorURCorner_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

        private void SelectorDLCorner_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorDLCorner_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Height + (curMousePos.Y - mousePos.Y) >= 0)
            {
                curShape.Height += (curMousePos.Y - mousePos.Y);
                this.Height += (curMousePos.Y - mousePos.Y);
                mousePos.Y = curMousePos.Y;
            }
            if (curShape.Width + (mousePos.X - curMousePos.X) >= 0)
            {
                curShape.Width += (mousePos.X - curMousePos.X);
                this.Width += (mousePos.X - curMousePos.X);
                curShape.SetValue(Canvas.LeftProperty, curMousePos.X + 3.0);
                this.SetValue(Canvas.LeftProperty, curMousePos.X);
                mousePos.X = curMousePos.X;
            }
            e.Handled = true;
        }

        private void SelectorDLCorner_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

        private void SelectorULCorner_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ButtonState == MouseButtonState.Pressed)
                mousePos = e.GetPosition(canva);
            Shape control = sender as Shape;
            control.CaptureMouse();
            e.Handled = true;
        }

        private void SelectorULCorner_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton != MouseButtonState.Pressed || mousePos == new Point(-1, -1))
                return;
            Point curMousePos = e.GetPosition(canva);
            if (curShape.Height + (mousePos.Y - curMousePos.Y) >= 0)
            {
                curShape.Height += (mousePos.Y - curMousePos.Y);
                this.Height += (mousePos.Y - curMousePos.Y);
                curShape.SetValue(Canvas.TopProperty, curMousePos.Y + 3.0);
                this.SetValue(Canvas.TopProperty, curMousePos.Y);
                mousePos.Y = curMousePos.Y;
            }
            if (curShape.Width + (mousePos.X - curMousePos.X) >= 0)
            {
                curShape.Width += (mousePos.X - curMousePos.X);
                this.Width += (mousePos.X - curMousePos.X);
                curShape.SetValue(Canvas.LeftProperty, curMousePos.X + 3.0);
                this.SetValue(Canvas.LeftProperty, curMousePos.X);
                mousePos.X = curMousePos.X;
            }
            e.Handled = true;
        }

        private void SelectorULCorner_MouseUp(object sender, MouseButtonEventArgs e)
        {
            mousePos = new Point(-1, -1);
            e.Handled = true;
            Shape control = sender as Shape;
            control.ReleaseMouseCapture();
        }

    }
}
